package com.lingnan.eduservice.controller;


import com.lingnan.common.utils.ResultEntity;
import com.lingnan.eduservice.entity.po.EduChapterPO;
import com.lingnan.eduservice.entity.vo.ChapterVO;
import com.lingnan.eduservice.service.EduChapterService;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 课程章节功能
 *
 * @author zzd
 * @since 2021-08-17
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Api(value = "课程章节功能")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 查询当前课程章节和小结(树型)
     *
     * @param courseId 课程号
     * @return
     */
    @GetMapping("findChapterVideoByCourseId/{courseId}")
    @ApiOperation("查询当前课程章节和小结(树型)")
    public ResultEntity findChapterVideoByCourseId(@PathVariable @ApiParam String courseId) {

        List<ChapterVO> chapterVOList = eduChapterService.findChapterVideoByCourseId(courseId);

        return ResultEntity.ok().data("chapterVOList", chapterVOList);


    }

    /**
     * 添加课程章节功能
     *
     * @param eduChapterPO
     * @return
     */
    @PostMapping("addChapter")
    @ApiOperation("添加课程章节")
    public ResultEntity addChapter(@RequestBody EduChapterPO eduChapterPO) {

        try {
            boolean flag = eduChapterService.save(eduChapterPO);
        } catch (Exception e) {
            e.printStackTrace();
            // 添加课程信息失败
            throw new LingNanException(20001, "添加课程章节信息失败");

        }
        return ResultEntity.ok();
    }

    @GetMapping("findChapterById/{chapterId}")
    @ApiOperation("根据章节ID查询章节信息")
    public ResultEntity findChapterById(@PathVariable String chapterId){

        EduChapterPO eduChapterPO = eduChapterService.getById(chapterId);
        // 查询不出结果
        if(eduChapterPO == null ){
            throw new LingNanException(20001,"查询当前章节信息失败");
        }
        return ResultEntity.ok().data("eduChapterPO",eduChapterPO);
    }

    @PostMapping("updateChapterById")
    @ApiOperation("根据章节ID修改章节信息")
    public ResultEntity updateChapterById(@RequestBody EduChapterPO eduChapterPO){

        // 执行修改
        try {
            eduChapterService.updateById(eduChapterPO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LingNanException(20001,"修改当前章节信息失败");
        }
        return ResultEntity.ok();
    }
    
    @DeleteMapping("deleteChapterById/{chapterId}")
    @ApiOperation("根据章节ID删除章节信息")
    public ResultEntity deleteChapterById(@PathVariable String chapterId){
        // 执行删除操作
        boolean flag = eduChapterService.deleteChapterById(chapterId);
        // 删除成功
        if(flag){
            return ResultEntity.ok();
        }
        // 删除失败
        return ResultEntity.error();
    }


}

